package com.xl.yygh.user.controller;

import com.xl.yygh.common.result.Result;
import com.xl.yygh.common.utils.AuthContextHolder;
import com.xl.yygh.common.utils.IpUtil;
import com.xl.yygh.model.model.user.UserInfo;
import com.xl.yygh.model.vo.user.LoginVo;
import com.xl.yygh.model.vo.user.UserAuthVo;
import com.xl.yygh.user.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author XiangLiang
 * @date 2023/11/1 15:37
 * @description
 */
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Resource
    private UserInfoService userInfoService;

    /**
     * 通过手机号进行登录
     *
     * @param loginVo 登录实体
     * @param request HTTP请求
     * @return 登陆成功返回用户名/手机号及token
     */
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        loginVo.setIp(IpUtil.getIpAddr(request));
        Map<String, Object> info = userInfoService.login(loginVo);
        return Result.ok(info);
    }

    /**
     * 用户认证接口
     *
     * @param userAuthVo 用户认证信息封装实体
     * @param request    请求
     * @return 认证结果
     */
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        // 传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    /**
     * 根据用户id获取用户信息接口
     *
     * @param request
     * @return
     */
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

}
